/**
 * 缓存状态管理器
 * @Author: wenjm
 * @Date: 2018-03-03 23:15:28
 * @Last Modified by: wenjm
 * @Last Modified time: 2021-07-12 19:14:17
 */
class Store extends Map {

  constructor(fw) {
    super();
    this.$framework = fw;
  }

  init() {
    this.$framework.db.contexts.each(item => {
      if (item.data) {
        super.set(item.key, item.data);
      }
    });
  }

  set(key, value) {
    super.set(key, value);
    this.$framework.db.contexts.put({
      key: key,
      data: value,
      date: new Date()
    });
  }

  async get(key) {
    let result = await this.$framework.db.contexts.get(key);
    return (result || {}).data;
  }

  remove(key) {
    this.$framework.db.contexts.delete(key);
  }

  clear() {
    this.$framework.db.contexts.clear().then(() => {
      super.clear();
    });
  }

}

export default Store;
